﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Physis.Core.Entity {

    public class EntityFactory : FactoryBase {

        #region Constructors

        public EntityFactory (Application forApplication) : base (forApplication) { return; }

        #endregion 


        #region Public Methods

        public Entity Entity (Int64 id, Boolean useCaching = false) {

            if (id == 0) { return null; }

            String cacheKey = "Physis.Application.Core.Entity.Entity." + id;

            Entity entity = null;

            ClearLastException ();

            try {

                if (useCaching) { entity = (Entity)CacheManager.GetObject (cacheKey); }

                if (entity == null) {

                    entity = new Entity (Application, id);

                    CacheManager.CacheObject (cacheKey, entity, CacheExpirationData);

                }

            }

            catch (Exception applicationException) {

                SetLastException (applicationException);

            }

            return entity;

        }

        #endregion 

    }

}
